package utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import java.util.StringTokenizer;
import org.biojava.bio.BioException;
import org.biojava.bio.seq.DNATools;
import org.biojava.bio.seq.RNATools;
import org.biojavax.Namespace;
import org.biojavax.RichObjectFactory;
import org.biojavax.bio.seq.RichSequence;
import org.biojavax.bio.seq.RichSequenceIterator;

public class Database {
	private List treeList=new ArrayList();
	private List namesList=new ArrayList();
	private List sequenceList=new ArrayList();
        Map organisms=new HashMap();
	
	public Database(String fileName) throws NoSuchElementException, BioException, FileNotFoundException{
		BufferedReader br = new BufferedReader(new FileReader(fileName));
		Namespace ns = RichObjectFactory.getDefaultNamespace();
		RichSequenceIterator seqs = RichSequence.IOTools.readFastaRNA(br, ns);
		int i=0;
		while (seqs.hasNext() ) {
			RichSequence rs = seqs.nextRichSequence();
			namesList.add(rs.getName());
                       
                        StringTokenizer tokenizer=new StringTokenizer(rs.getDescription());
                     
                        tokenizer.nextToken();
                        tokenizer.nextToken();
                        tokenizer.nextToken();
                        int num=0;
                        String name=tokenizer.nextToken();
                       while (tokenizer.hasMoreTokens() && num<1){
                            name=name+" "+ tokenizer.nextToken();
                            num++;
                       }
                       
                        if (name.charAt(0)>='A'){
                            ArrayList se;
                           if ((se=(ArrayList)organisms.get(name))!=null){
                               se.add(rs.seqString());
                           }else{
                               se=new ArrayList();
                               se.add(rs.seqString());
                               organisms.put(name,se);
                           }
                        }
			sequenceList.add(rs.seqString());
			if (i%1000==0) System.out.println(i); 
                            i++;
		}	
		System.out.println("----------------------------------------------");
            System.out.println("number of organisms:"+organisms.size());
                System.out.println("number of sequences:"+sequenceList.size());
                System.out.println("-------END of DATABASE------------");
            /*        
                Iterator iter=organisms.keySet().iterator();
                while (iter.hasNext()){
                    String key=(String) iter.next();
                    System.out.println(key+" "+((ArrayList)organisms.get(key)).size());
                }*/
	}
        public Map getOrganisms(){
            return organisms;
        }
	
	public int sizeOLD(){
		return namesList.size();
	}
	public String getSequenceOLD(int index){
		return (String) sequenceList.get(index);
	}

	public String getNameOLD(int index){
		return (String) namesList.get(index);
	}
	
}
